package cn.zhl.lambda;

import org.junit.Test;

import java.util.Comparator;
import java.util.function.BinaryOperator;
import java.util.function.Supplier;

/**
 * @program: demos
 * @description:
 * @author: 刘振华
 * @create: 2020-04-21 15:55
 **/
public class SupplierTest {
	@Test
	public void test1() {
		System.out.println(sup1(() -> 10));
	}

	private String sup1(Supplier<Integer> i) {
		return "hello" + i.get();
	}

	@Test
	public void test2() {
		System.out.println(bi(2, 3, (a, b) -> a + b));
	}

	private Integer bi(Integer a1, Integer a2, BinaryOperator<Integer> bo) {
		return bo.apply(a1, a2);
	}

	@Test
	public void test3() {
		Person p1 = Person.builder().name("小明").age(10).build();
		Person p2 = Person.builder().name("小红").age(12).build();
		System.out.println(minPerson(p1, p2, (o1, o2) -> o2.getAge() - o1.getAge()));
		System.out.println(minPerson(p1, p2, Comparator.comparingInt(Person::getAge)));
	}

	private Person minPerson(Person p1, Person p2, Comparator<Person> cp) {
		return BinaryOperator.minBy(cp).apply(p1, p2);
	}
}
